
import { getUserInfo, login, getUserDetailById } from '@/api/user'
import { getToken, setToken, removeToken } from '@/utils/auth'
const state = {
  token: getToken() || '',
  userInfo: {}
}

const mutations = {
  setToken(state, token) {
    state.token = token
    console.log('这是本地存储', setToken(token))
  },
  setUserInfo(state, newUserInfo) {
    state.userInfo = newUserInfo
  },
  logout(state) {
    removeToken(state.token)
    state.token = ''
    state.userInfo = {}
  }
}

const actions = {
  async loginIn(context, loginForm) {
    const res = await login(loginForm)
    console.log('这是res', res)
    context.commit('setToken', res)
    localStorage.setItem('logTime', Date.now())
  },
  async getUserInfo(context) {
    const res = await getUserInfo()
    const detail = await getUserDetailById(res.userId)
    // console.log(res)
    context.commit('setUserInfo', { ...res, ...detail })
  }
}
export default {
  namespaced: true,
  state,
  mutations,
  actions
}

